/*
 *    Alliciga's Process Partner Project
 * 
 *  Copyright (C) 2006-2011 Alliciga <alliciga@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
 
#include "../include/UserInterfaceThread.h"
#include "../include/MonitorPolicy.h"
#include "../include/NotificationPolicy.h"

UserInterfaceThread::UserInterfaceThread(ppp &_ppp)
	: appp(_ppp)
{
}

UserInterfaceThread::~UserInterfaceThread()
{
}

void UserInterfaceThread::MainLoop()
{
	while(!closing)
	{
		cout << "command>";
		string cmd;
		cin >> cmd;
		if(cmd=="quit")
			close();
		if(cmd=="add")
			HandleAdd();
		if(cmd=="help")
			HandleHelp();
		if(cmd=="addmp")
			HandleAddMP();
		if(cmd=="addnp")
			HandleAddNP();
		if(cmd=="del")
			HandleDel();
		if(cmd=="delmp")
			HandleDelMP();
		if(cmd=="delnp")
			HandleDelNP();
		if(cmd=="show")
			HandleShow();
		if(cmd=="showmp")
			HandleShowMP();
		if(cmd=="shownp")
			HandleShowNP();
		if(cmd=="amp")
			HandleAMP();
		if(cmd=="anp")
			HandleANP();
	}
	cout << "UserInterfaceThread::MainLoop terminated.." << endl;
}

void UserInterfaceThread::HandleHelp()
{
	cout << "add     -- add a MonitorThread to monitor a specified Process" << endl;
	cout << "addmp   -- add specified MonitorPolicy" << endl;
	cout << "addnp   -- add specified NotificationPolicy" << endl;
	cout << "del     -- delete specified monitored Process" << endl;
	cout << "delmp   -- delete specified MonitorPolicy" << endl;
	cout << "delnp   -- delete specified NotificationPolicy" << endl;
	cout << "show    -- show current monitored Process" << endl;
	cout << "showmp  -- show current using MonitorPolicy" << endl;
	cout << "shownp  -- show current using NotificationPolicy" << endl;
	cout << "amp     -- show available MonitorPolicy" << endl;
	cout << "anp     -- show available NotificationPolicy" << endl;
	cout << "help    -- show this message" << endl;
	cout << "quit    -- delete all MonitorThread and quit from the Appp" << endl;
}

void UserInterfaceThread::HandleAdd()
{
	cout << endl << "Enter target Process ID:";
	int pid;
	cin >> pid;
	if(!appp.AddMonitorProcess(pid))
		cout << "Action Failed. Invalid pid." << endl;
}

void UserInterfaceThread::HandleAddMP()
{
	int pid;
	string mpName;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	cout << "Enter MonitorPolicyName:";
	cin >> mpName;
	if(!appp.AddMonitorPolicy(pid,mpName))
		cout << "Action Failed. Invalid PID or MonitorPolicyName." << endl;
}

void UserInterfaceThread::HandleAddNP()
{
	int pid;
	string mpName;
	string npName;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	cout << "Enter MonitorPolicyName:";
	cin >> mpName;
	cout << "Enter NotificationName:";
	cin >> npName;
	if(!appp.AddNotificationPolicy(pid,mpName,npName))
		cout << "Action Failed. Invalid pid or MonitorPolicyName or NotificationName." << endl;
}

void UserInterfaceThread::HandleDel()
{
	int pid;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	if(!appp.DeleteMonitorProcess(pid))
		cout << "Action Failed. Invalid pid." << endl;
}

void UserInterfaceThread::HandleDelMP()
{
	int pid;
	string mpName;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	cout << "Enter MonitorPolicyName:";
	cin >> mpName;
	if(!appp.DeleteMonitorPolicy(pid,mpName))
		cout << "Action Failed. Invalid PID or MonitorPolicyName." << endl;
}

void UserInterfaceThread::HandleDelNP()
{
	int pid;
	string mpName;
	string npName;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	cout << "Enter MonitorPolicyName:";
	cin >> mpName;
	cout << "Enter NotificationName:";
	cin >> npName;
	if(!appp.DeleteNotificationPolicy(pid,mpName,npName))
		cout << "Action Failed. Invalid pid or MonitorPolicyName or NotificationName." << endl;
}

void UserInterfaceThread::HandleShow()
{
	list<string> tmp;
	tmp = appp.GetMonitorProcessList();
	list<string>::iterator r = tmp.begin();
	while(tmp.end() != r)
	{
		cout << *r << endl;
		r++;
	}
}

void UserInterfaceThread::HandleShowMP()
{
	int pid;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	list<string> tmp;
	tmp = appp.GetMonitorPolicyList(pid);
	list<string>::iterator r = tmp.begin();
	while(tmp.end() != r)
	{
		cout << *r << endl;
		r++;
	}
}

void UserInterfaceThread::HandleShowNP()
{
	int pid;
	string mpName;
	cout << "Enter MonitorProcessID:";
	cin >> pid;
	cout << "Enter MonitorPolicyName:";
	cin >> mpName;
	list<string> tmp;
	tmp = appp.GetNotificationPolicyList(pid,mpName);
	list<string>::iterator r = tmp.begin();
	while(tmp.end() != r)
	{
		cout << *r << endl;
		r++;
	}
}

void UserInterfaceThread::HandleAMP()
{
	list<string> tmp;
	tmp = appp.GetAvailableMonitorPolicy();
	list<string>::iterator r = tmp.begin();
	while(tmp.end() != r)
	{
		cout << *r << endl;
		r++;
	}
}

void UserInterfaceThread::HandleANP()
{
	list<string> tmp;
	tmp = appp.GetAvailableNotificationPolicy();
	list<string>::iterator r = tmp.begin();
	while(tmp.end() != r)
	{
		cout << *r << endl;
		r++;
	}
}

// ---------- End of File ------------

